<?php
namespace app\admin\logic;
use app\admin\code\BaseCode;
use app\admin\model\NavModel;
use think\Lang;

/**
 * 导航，菜单相关逻辑
 * Class NavLogic
 * @package app\admin\logic
 */
class MenuLogic
{
    /**
     * 获取左侧菜单逻辑
     * @param $adminAuth 当前管理员拥有的权限
     * @param $module 当前执行的模块
     * @param $controller 当前执行的控制器
     * @param $action 当前执行的动作
     */
    public function get_menu($adminAuth,$module,$controller,$action){
        $module = strtolower($module);
        $controller = strtolower($controller);
        $action = strtolower($action);


        $where = array('is_menu'=>1);
        $admin = session('admin');
        if($admin['rule'] &&  !FOUNDER){
            $where['id'] = array('in',$admin['rule']);
        }
        $model = new AuthModel();
        $menuList = $model->selectData($where,'*','sort asc');
        $temp = array();

        usort($menuList,function($a,$b){
            if($a['pid'] == 0 && $a['sort'] < $b['sort']){
                return false;
            }else{
                return true;
            }
        });
        foreach ($menuList as $k=>$v){
            if($v['module'] == $module && $v['controller'] == $controller ){
                $v['is_select'] = 1;
            }else{
                $v['is_select'] = 0;
            }
            if($v['pid'] == 0){
                if(isset($temp[$v['id']]['parent']['is_select'])){
                    $v['is_select'] = $temp[$v['id']]['parent']['is_select'];
                }
                $temp[$v['id']]['parent'] = $v;
            }else{
                if(!isset($temp[$v['pid']])){
                    $temp[$v['pid']]['parent'] = [];
                }
                $temp[$v['pid']]['child'][] = $v;
                if($v['is_select'] == 1 &&
                    (!isset($temp[$v['pid']]['parent']['is_select']) ||
                        $temp[$v['pid']]['parent']['is_select'] == 0)){
                    $temp[$v['pid']]['parent']['is_select'] = 1;
                }
            }
        }
        $menuList = $temp;
        $this->assign('menuList',$menuList);
    }
}


